package com.fallsvc.re.networkprogramming.file.output;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @auther falls_vc
 * description:
 * @date 2025/11/3  16:19
 */
public class Demo2 {
    public static void main1(String[] args) throws IOException {
        OutputStream os = new FileOutputStream("output.txt");
//        OutputStreamWriter osWriter = new OutputStreamWriter(os, "utf-8"); // 告诉
//        PrintWriter writer = new PrintWriter(osWriter);
        PrintWriter writer = new PrintWriter(os,false, StandardCharsets.UTF_8);
// 接下来我们就可以⽅便的使⽤ writer 提供的各种⽅法了
        writer.print("Hello");
        writer.println("你好");
        writer.printf("%d: %s\n", 1, "没什么");
// 不要忘记 flush
        writer.flush();
        writer.close();
//        osWriter.close();
        os.close();
    }
    public static void main(String[] args) throws IOException {
        try (OutputStream os = new FileOutputStream("output.txt")) {
            try (OutputStreamWriter osWriter = new OutputStreamWriter(os, "UTF-8")){
                try (PrintWriter writer = new PrintWriter(osWriter)) {
                writer.println("我是第⼀⾏");
                writer.print("我的第⼆⾏\r\n");
                writer.printf("%d: 我的第三⾏\r\n", 1 + 1);
                writer.flush();
            }
        }
    }
}
}
